<template>
  <p>
    <a-space>
      <train-select v-model:trainCode="selectTrainCode" width="200px"></train-select>
      <a-button type="primary" @click="handleQuery">查询</a-button>
      <a-button type="primary" @click="onAdd">新增</a-button>
    </a-space>
  </p>
  <a-table :dataSource="trainCarriages"
           :columns="columns"
           :pagination="pagination"
           @change="handleTableChange"
           :loading="loading">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'operation'">
        <a-space>
          <a-popconfirm
              title="删除后不可恢复，确认删除?"
              @confirm="onDelete(record)"
              ok-text="确认" cancel-text="取消">
            <a style="color: red">删除</a>
          </a-popconfirm>
          <a @click="onEdit(record)">编辑</a>
        </a-space>
      </template>
      <template v-else-if="column.dataIndex === 'seatType'">
        <span v-for="item in SEAT_TYPE_ARRAY" :key="item.code">
          <span v-if="item.code === record.seatType">
            {{ item.desc }}
          </span>
        </span>
      </template>
    </template>
  </a-table>
  <a-modal v-model:visible="visible" title="火车车厢" @ok="handleOk"
           ok-text="确认" cancel-text="取消">
    <a-form :model="trainCarriage" :label-col="{span: 4}" :wrapper-col="{ span: 20 }">
      <a-form-item label="车次编号">
        <train-select v-model:trainCode="trainCarriage.trainCode"></train-select>
      </a-form-item>
      <a-form-item label="厢号">
        <a-input v-model:value="trainCarriage.index"/>
      </a-form-item>
      <a-form-item label="座位类型">
        <a-select v-model:value="trainCarriage.seatType">
          <a-select-option v-for="item in SEAT_TYPE_ARRAY" :key="item.code" :value="item.code">
            {{ item.desc }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="排数">
        <a-input v-model:value="trainCarriage.rowCount"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup lang="ts" name="train-carriage-view">
import {ref, onMounted} from 'vue';
import {notification} from "ant-design-vue";
import {save, query, deleteById} from "@/api/base/carriage";
import {QueryParams, SaveParams} from "@/api/base/carriage/type";
import {copyObj} from "@/utils/tools";
import {SEAT_TYPE_ARRAY} from "@/enums/SeatType";
import TrainSelect from "@/components/train-select.vue";

const visible = ref(false);
let trainCarriage = ref({
  id: "",
  trainCode: "",
  index: "",
  seatType: "",
  seatCount: "",
  rowCount: "",
  createTime: "",
  updateTime: "",
});
const trainCarriages = ref([]);
// 分页的三个属性名是固定的
const pagination = ref({
  total: 0,
  current: 1,
  pageSize: 15,
});
let loading = ref(false);
const columns = [
  {
    title: '车次编号',
    dataIndex: 'trainCode',
    key: 'trainCode',
  },
  {
    title: '厢号',
    dataIndex: 'index',
    key: 'index',
  },
  {
    title: '座位类型',
    dataIndex: 'seatType',
    key: 'seatType',
  },
  {
    title: '座位数',
    dataIndex: 'seatCount',
    key: 'seatCount',
  },
  {
    title: '排数',
    dataIndex: 'rowCount',
    key: 'rowCount',
  },
  {
    title: '操作',
    dataIndex: 'operation'
  }
];
const selectTrainCode = ref<string|undefined>(undefined);
const onAdd = () => {
  trainCarriage.value = {
    createTime: "",
    id: "",
    index: "",
    rowCount: "",
    seatCount: "",
    seatType: "",
    trainCode: "",
    updateTime: ""
  };
  visible.value = true;
};

const onEdit = (record: SaveParams) => {
  trainCarriage.value = copyObj(record);
  visible.value = true;
};

const onDelete = async (record: SaveParams) => {
  try {
    const result = await deleteById(record.id as number);
    if (result.success) {
      notification.success({message: "操作成功！", description: "删除成功！"});
      await handleQuery({
        pageNum: pagination.value.current,
        pageSize: pagination.value.pageSize,
      });
    } else {
      notification.error({message: undefined, description: result.message});
    }
  } catch (e: any) {
    notification.error({message: undefined, description: e.message});
  }
};

const handleOk = async () => {
  try {
    const result = await save(trainCarriage.value as any);
    if (result.success) {
      notification.success({message: "success", description: "保存成功！"});
      visible.value = false;
      await handleQuery({
        pageNum: pagination.value.current,
        pageSize: pagination.value.pageSize
      });
    } else {
      notification.error({message: "失败", description: result.message});
    }
  } catch (e: any) {
    notification.error({message: "失败", description: e.message});
  }
};

const handleQuery = async (param: QueryParams) => {
  if (!param) {
    param = {
      pageNum: 1,
      pageSize: pagination.value.pageSize
    };
  }
  loading.value = true;
  try {
    const result = await query({...param, trainCode: selectTrainCode.value})
    loading.value = false;
    if (result.success) {
      trainCarriages.value = result.data as any;
      // 设置分页控件的值
      pagination.value.current = param.pageNum as number;
      pagination.value.total = result.total;
    } else {
      notification.error({message: "失败", description: result.message});
    }
  } catch (e: any) {
    notification.error({message: "失败", description: e.message});
  }
};

const handleTableChange = (page: any) => {
  // console.log("看看自带的分页参数都有啥：" + JSON.stringify(page));
  pagination.value.pageSize = page.pageSize;
  handleQuery({
    pageNum: page.current,
    pageSize: page.pageSize
  });
};

onMounted(() => {
  handleQuery({
    pageNum: 1,
    pageSize: pagination.value.pageSize
  });
})
</script>
